package com.example.fw;

import java.util.Properties;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;

public class ApplicationManager {
	
	public WebDriver driver;
	public String baseUrl;
	private Properties properties;
	private HibernateHelper hebirnateHelper;
	private AccountHelper accountHelper;
	private MailHelper mailHelper;
	private JamesHelper jamesHelper;
	private FtpHelper ftpHelper;
	private AdminHelper adminHelper;
	
	public ApplicationManager(Properties properties) {
		this.properties = properties;
	}
	
	public void stop() {
		driver.quit();
		String ftpConfigFile = properties.getProperty("ftp.configFile");
		ftpHelper.restoreFile(ftpConfigFile + ".bak", ftpConfigFile);
		ftpHelper.closeFtpConnection();
	}
	
	public WebDriver getDriver() {
		String browser = properties.getProperty("browser");
		if (driver == null) {
			long timeout = Long.valueOf(properties.getProperty("timeout"));
			if ("firefox".equals(browser)) {
				driver = new FirefoxDriver();
			} else if ("ie".equals(browser)) {
				driver = new InternetExplorerDriver();
			} else {
				throw new Error("Unsupported browser: " + browser);
			}
			baseUrl = properties.getProperty("baseUrl");
		    driver.manage().timeouts().implicitlyWait(timeout, TimeUnit.SECONDS);
			driver.get(baseUrl);
		}
		return driver;
	}

	public HibernateHelper getHebirnateHelper() {
		if (hebirnateHelper == null) {
			hebirnateHelper = new HibernateHelper(this);
		}
		return hebirnateHelper;
	}
	
	public String getProperty(String key) {
		return properties.getProperty(key);
	}

	public AccountHelper getAccountHelper() {
		if (accountHelper == null) {
			accountHelper = new AccountHelper(this);
		}
		return accountHelper;
	}

	public MailHelper getMailHelper() {
		if (mailHelper == null) {
			mailHelper = new MailHelper(this);
		}
		return mailHelper;
	}
	
	public JamesHelper getJamesHelper() {
		if (jamesHelper == null) {
			jamesHelper = new JamesHelper(this);
		}
		return jamesHelper;
	}
	
	public FtpHelper getFtpHelper() {
		if (ftpHelper == null) {
			ftpHelper = new FtpHelper(this);
		}
		return ftpHelper;
	}

	public AdminHelper getAdminHelper() {
		if (adminHelper == null) {
			adminHelper = new AdminHelper(this);
		}
		return adminHelper;
	}
}
